//
//  WSQBaseNavigationController.m
//  WSQOCTools
//
//  Created by 翁胜琼 on 2018/8/28.
//  Copyright © 2018年 Dev John. All rights reserved.
//

#import "WSQBaseNavigationController.h"

@interface WSQBaseNavigationController ()

@end

@implementation WSQBaseNavigationController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self setupUI];
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if (self.childViewControllers.count >= 1) {
        //返回按钮自定义
        UIBarButtonItem *navigationSpacer = [self setupChildViewControllerBackSpacerItem];
        
        UIButton *button = [self setupChildViewControllerBackBtn];
        UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:button];
        
        viewController.navigationItem.leftBarButtonItems = @[navigationSpacer, backButton];
        viewController.hidesBottomBarWhenPushed = YES;
        
        // 就有滑动返回功能
        self.interactivePopGestureRecognizer.delegate = nil;
    }
    
    [super pushViewController:viewController animated:true];

}

// 设置子控制器的返回按钮
- (UIButton *)setupChildViewControllerBackBtn
{
    UIButton *button = [[UIButton alloc] init];
    button.frame = CGRectMake(0, 0, 33, 33);
    [button setImage:[UIImage imageNamed:@"navigation_back_normal"] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:@"navigation_back_hl"] forState:UIControlStateHighlighted];
    [button addTarget:self action:@selector(backButtonTapClick) forControlEvents:UIControlEventTouchUpInside];
    
    // 向左偏移
    if (@available(ios 11.0,*)) {
        button.imageEdgeInsets = UIEdgeInsetsMake(0, -10,0, 0);
        button.contentEdgeInsets = UIEdgeInsetsMake(0, -15,0, 0);
    }
    
    return button;
}

// 设置子控制器返回按钮左侧的间隔
- (UIBarButtonItem *)setupChildViewControllerBackSpacerItem
{
    UIBarButtonItem *navigationSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    navigationSpacer.width = -15;
    
    return navigationSpacer;
}

- (void)backButtonTapClick
{
    
    [self popViewControllerAnimated:YES];
}

- (void)dealloc
{
    //NSStringFromSelector返回该方法的方法名
    NSLog(@"Runing %@ %@",[self class], NSStringFromSelector(_cmd));
}

// MARK: - setupUI
- (void)setupUI
{
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationBar.barTintColor = [UIColor whiteColor];
    [self setupNavigationBarTitleAttribute];
}

// 设置导航栏标题样式
- (void)setupNavigationBarTitleAttribute
{
    NSMutableDictionary *textAttributes = [NSMutableDictionary dictionary];
    textAttributes[NSForegroundColorAttributeName] = [UIColor colorWithRed:43/255 green:45/255 blue:43/255 alpha:1];
    textAttributes[NSFontAttributeName] = [UIFont fontWithName:@"PingFang SC" size:16];
    
    NSShadow *shadow = [[NSShadow alloc]init];
    shadow.shadowOffset = CGSizeZero;
    textAttributes[NSShadowAttributeName] = shadow;
    
    [[UIBarButtonItem appearance] setTitleTextAttributes:textAttributes forState:UIControlStateNormal];
    
}

// 设置导航栏样式
- (void)setupNavigationBarApperance
{
    // 2.设置导航栏的背景图片
    NSString *navBarBg = @"navigationbar_bg";
    [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:navBarBg] forBarMetrics:UIBarMetricsDefault];
    
    
    // 3. 设置导航栏的渐变色（iOS7中返回箭头的颜色）
    [UINavigationBar appearance].tintColor = [UIColor colorWithRed:43/255 green:45/255 blue:43/255 alpha:1];
    [UINavigationBar appearance].backgroundColor = [UIColor whiteColor];
    [UINavigationBar appearance].translucent = false;
    
    // 3.1 隐藏系统分割线
    [UINavigationBar appearance].shadowImage = [UIImage new];
    
    // 4.设置导航栏标题颜色
    NSMutableDictionary *textAttributes = [NSMutableDictionary dictionary];
    textAttributes[NSForegroundColorAttributeName] = [UIColor colorWithRed:51/255 green:51/255 blue:51/255 alpha:1];
    textAttributes[NSFontAttributeName] = [UIFont fontWithName:@"PingFang SC" size:18];
    
    // 5.去除阴影
    NSShadow *shadow = [[NSShadow alloc]init];
    shadow.shadowOffset = CGSizeZero;
    textAttributes[NSShadowAttributeName] = shadow;
    [[UINavigationBar appearance] setTitleTextAttributes:textAttributes];
}

// 指定导航栏样式
-(UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleDefault;
}

// 告知控制器导航栏样式从子控制器获取
- (UIViewController *)childViewControllerForStatusBarStyle
{
    return self.topViewController;
}

// 旋屏支持
- (BOOL)shouldAutorotate
{
    return self.topViewController.shouldAutorotate;
}
@end
